package com.lashou.chunshen.lashoutuangou.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lashou.chunshen.lashoutuangou.R;
import com.lashou.chunshen.lashoutuangou.bean.BaseBean;
import com.lashou.chunshen.lashoutuangou.bean.HomeBanner;
import com.lashou.chunshen.lashoutuangou.tools.PicassoUtils;

/**
 * 首页自动轮播banner适配器
 * Created by momo on 2016/9/7.
 */

public  class HomeViewpagerbannerAdapter extends PagerAdapter {
    private Context mContext;
    private BaseBean data;
    private ViewPager viewpager;

    public HomeViewpagerbannerAdapter(Context context, BaseBean data) {
        this.mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data != null) {
            //            return viewList.size();
            //第一处修改，设置轮播最大值，等于无限循环
            return Integer.MAX_VALUE;
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 初始化position位置的界面
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        //第二处修改，当前要显示的数据索引为集合长度
        int size = ((HomeBanner) data).getResults().getHeader_banner().size();
        int newPosition = position % size;
        View v = LayoutInflater.from(mContext).inflate(R.layout.home_banner_item, null);
        ImageView iv = (ImageView) v.findViewById(R.id.iv_home_banner);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        PicassoUtils.loadImageViewSize(mContext, ((HomeBanner) data).getResults().getHeader_banner().get(newPosition).getImg_big(), 750, 280, iv);
        container.addView(v);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((View) object);
    }


    public void setViewpager(ViewPager viewpager) {
        this.viewpager = viewpager;
    }

    public void openAutoLoop() {
        final boolean[] isLooper = new boolean[1];
        //开启一个线程，用于循环
        new Thread(new Runnable() {
            @Override
            public void run() {
                isLooper[0] = true;
                while (isLooper[0]) {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    ((Activity) mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //更新UI
                            viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
                        }
                    });
                }

            }
        }).start();
    }

}